

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>openchem.utils.graph &mdash; OpenChem 0.1 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script src="../../../_static/jquery.js"></script>
        <script src="../../../_static/underscore.js"></script>
        <script src="../../../_static/doctools.js"></script>
        <script src="../../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html">
          

          
            
            <img src="../../../_static/logo.png" class="logo" alt="Logo"/>
          
          </a>

          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../../../index.html">Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../installation_instructions.html">Installation instructions</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../../installation_instructions.html#general-installation">General installation</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../../installation_instructions.html#installation-with-docker">Installation with Docker</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../../how_to_run_tutorial.html">How to define and train models</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../../how_to_run_tutorial.html#arguments-for-launch-py">Arguments for launch.py</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../../how_to_run_tutorial.html#arguments-for-run-py">Arguments for run.py</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../../how_to_run_tutorial.html#configuration-file">Configuration file</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../../how_to_run_tutorial.html#launching-jobs">Launching jobs</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../../tutorials/blocks.html">Tutorials and Recipes</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../../tutorials/getting_started.html">Getting started with building models in OpenChem</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../../tutorials/getting_started.html#loading-data">Loading data</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../tutorials/getting_started.html#creating-pytorch-dataset">Creating PyTorch dataset</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../tutorials/getting_started.html#creating-openchem-model-and-specifying-parameters">Creating OpenChem model and specifying parameters</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../tutorials/getting_started.html#training-the-model">Training the model</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../../tutorials/gcnn_tutorial.html">GraphCNN for predicting logP</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../../tutorials/gcnn_tutorial.html#defining-node-attributes">Defining node attributes</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../tutorials/gcnn_tutorial.html#loading-data">Loading data</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../tutorials/gcnn_tutorial.html#defining-model-architechture">Defining model architechture</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../tutorials/gcnn_tutorial.html#training-and-evaluating-the-model">Training and evaluating the model</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../../tutorials/tox21_tutorial.html">Tox21 Challenge</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../../tutorials/tox21_tutorial.html#loading-data">Loading data</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../tutorials/tox21_tutorial.html#defining-evaluation-function">Defining evaluation function</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../tutorials/tox21_tutorial.html#defining-model-architechture">Defining model architechture</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../tutorials/tox21_tutorial.html#training-and-evaluating-the-model">Training and evaluating the model</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../../api-docs/blocks.html">API documentation</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../../../api-docs/models.html">models</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/models.html#module-models.openchem_model">openchem_model</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/models.html#module-models.Smiles2Label">Smiles2Label</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/models.html#module-models.Graph2Label">Graph2Label</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/models.html#module-models.MoleculeProtein2Label">MoleculeProtein2Label</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/models.html#vanilla-model">vanilla_model</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../../api-docs/modules.html">modules</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/modules.encoders.html">encoders</a><ul>
<li class="toctree-l4"><a class="reference internal" href="../../../api-docs/modules.encoders.html#module-modules.encoders.openchem_encoder">openchem_encoder</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../../api-docs/modules.encoders.html#module-modules.encoders.rnn_encoder">rnn_encoder</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../../api-docs/modules.encoders.html#cnn-encoder">cnn_encoder</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../../api-docs/modules.encoders.html#gcnn-encoder">gcnn_encoder</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/modules.embeddings.html">embeddings</a><ul>
<li class="toctree-l4"><a class="reference internal" href="../../../api-docs/modules.embeddings.html#module-modules.embeddings.openchem_embedding">openchem_embedding</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../../api-docs/modules.embeddings.html#module-modules.embeddings.basic_embedding">basic_embedding</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../../api-docs/modules.embeddings.html#positional-embedding">positional_embedding</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/modules.mlp.html">mlp</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../../api-docs/layers.html">layers</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/layers.html#module-layers.conv_bn_relu">conv_bn_relu</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/layers.html#module-layers.gcn">gcn</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../../api-docs/data.html">data</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/data.html#module-data.smiles_data_layer">smiles_data_layer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/data.html#graph-data-layer">graph_data_layer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/data.html#module-data.smiles_protein_data_layer">smiles_protein_data_layer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/data.html#module-data.vanilla_data_layer">vanilla_data_layer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/data.html#module-data.smiles_enumerator">smiles_enumerator</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/data.html#module-data.utils">utils</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../../api-docs/criterion.html">criterion</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/criterion.html#module-openchem.criterion.multitask_loss">multitask_loss</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../../api-docs/optimizer.html">optimizer</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/optimizer.html#module-optimizer.openchem_optimizer">openchem_optimizer</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/optimizer.html#module-optimizer.openchem_lr_scheduler">openchem_lr_scheduler</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../../api-docs/utils.html">utils</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/utils.html#module-openchem.utils.graph">graph</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/utils.html#id1">utils</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../../api-docs/utils.html#logger">logger</a></li>
</ul>
</li>
</ul>
</li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">OpenChem</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>openchem.utils.graph</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for openchem.utils.graph</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">rdkit</span> <span class="kn">import</span> <span class="n">Chem</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>


<div class="viewcode-block" id="Attribute"><a class="viewcode-back" href="../../../api-docs/utils.html#openchem.utils.graph.Attribute">[docs]</a><span class="k">class</span> <span class="nc">Attribute</span><span class="p">:</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attr_type</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">one_hot</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">values</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">attr_type</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;node&#39;</span><span class="p">,</span> <span class="s1">&#39;edge&#39;</span><span class="p">]:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Invalid value for attribute type: must be &quot;node&quot; &#39;</span> <span class="s1">&#39;or &quot;edge&quot;&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">attr_type</span> <span class="o">=</span> <span class="n">attr_type</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
        <span class="k">if</span> <span class="n">values</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">n_values</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">values</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">attr_values</span> <span class="o">=</span> <span class="n">values</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">one_hot</span> <span class="o">=</span> <span class="n">one_hot</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">one_hot</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">one_hot_dict</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">n_values</span><span class="p">):</span>
                <span class="n">tmp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">n_values</span><span class="p">)</span>
                <span class="n">tmp</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">one_hot_dict</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">attr_values</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span> <span class="o">=</span> <span class="n">tmp</span></div>


<div class="viewcode-block" id="Node"><a class="viewcode-back" href="../../../api-docs/utils.html#openchem.utils.graph.Node">[docs]</a><span class="k">class</span> <span class="nc">Node</span><span class="p">:</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">idx</span><span class="p">,</span> <span class="n">rdmol</span><span class="p">,</span> <span class="n">get_atom_attributes</span><span class="p">,</span> <span class="n">has_3D</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="n">rdatom</span> <span class="o">=</span> <span class="n">rdmol</span><span class="o">.</span><span class="n">GetAtoms</span><span class="p">()[</span><span class="n">idx</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">node_idx</span> <span class="o">=</span> <span class="n">idx</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">atom_type</span> <span class="o">=</span> <span class="n">rdatom</span><span class="o">.</span><span class="n">GetAtomicNum</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">attributes_dict</span> <span class="o">=</span> <span class="n">get_atom_attributes</span><span class="p">(</span><span class="n">rdatom</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">has_3D</span><span class="p">:</span>
            <span class="n">pos</span> <span class="o">=</span> <span class="n">rdmol</span><span class="o">.</span><span class="n">GetConformer</span><span class="p">()</span><span class="o">.</span><span class="n">GetAtomPosition</span><span class="p">(</span><span class="n">idx</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pos_x</span> <span class="o">=</span> <span class="n">pos</span><span class="o">.</span><span class="n">x</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pos_y</span> <span class="o">=</span> <span class="n">pos</span><span class="o">.</span><span class="n">y</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pos_z</span> <span class="o">=</span> <span class="n">pos</span><span class="o">.</span><span class="n">z</span></div>

<div class="viewcode-block" id="Edge"><a class="viewcode-back" href="../../../api-docs/utils.html#openchem.utils.graph.Edge">[docs]</a><span class="k">class</span> <span class="nc">Edge</span><span class="p">:</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">rdbond</span><span class="p">,</span> <span class="n">get_bond_attributes</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">begin_atom_idx</span> <span class="o">=</span> <span class="n">rdbond</span><span class="o">.</span><span class="n">GetBeginAtomIdx</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">end_atom_idx</span> <span class="o">=</span> <span class="n">rdbond</span><span class="o">.</span><span class="n">GetEndAtomIdx</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">get_bond_attributes</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">attributes_dict</span> <span class="o">=</span> <span class="n">get_bond_attributes</span><span class="p">(</span><span class="n">rdbond</span><span class="p">)</span></div>


<div class="viewcode-block" id="Graph"><a class="viewcode-back" href="../../../api-docs/utils.html#openchem.utils.graph.Graph">[docs]</a><span class="k">class</span> <span class="nc">Graph</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;Describes an undirected graph class&quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mol</span><span class="p">,</span> <span class="n">max_size</span><span class="p">,</span> <span class="n">get_atom_attributes</span><span class="p">,</span> <span class="n">get_bond_attributes</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">kekulize</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">addHs</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> 
                 <span class="n">has_3D</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">from_rdmol</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">kekulize</span> <span class="o">=</span> <span class="n">kekulize</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">addHs</span> <span class="o">=</span> <span class="n">addHs</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">has_3D</span> <span class="o">=</span> <span class="n">has_3D</span>
        <span class="k">if</span> <span class="n">from_rdmol</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">smiles</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolToSmiles</span><span class="p">(</span><span class="n">mol</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">smiles</span> <span class="o">=</span> <span class="n">mol</span>
        <span class="k">if</span> <span class="n">from_rdmol</span><span class="p">:</span>
            <span class="n">rdmol</span> <span class="o">=</span> <span class="n">mol</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">rdmol</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">MolFromSmiles</span><span class="p">(</span><span class="n">mol</span><span class="p">)</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">addHs</span><span class="p">:</span>
                <span class="n">rdmol</span> <span class="o">=</span> <span class="n">Chem</span><span class="o">.</span><span class="n">AddHs</span><span class="p">(</span><span class="n">rdmol</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">kekulize</span><span class="p">:</span>
            <span class="n">Chem</span><span class="o">.</span><span class="n">Kekulize</span><span class="p">(</span><span class="n">rdmol</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">num_nodes</span> <span class="o">=</span> <span class="n">rdmol</span><span class="o">.</span><span class="n">GetNumAtoms</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">num_edges</span> <span class="o">=</span> <span class="n">rdmol</span><span class="o">.</span><span class="n">GetNumBonds</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">nodes</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="n">has_3D</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">xyz</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">max_size</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>
        <span class="n">num_atoms</span> <span class="o">=</span> <span class="n">rdmol</span><span class="o">.</span><span class="n">GetNumAtoms</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">num_atoms</span><span class="p">):</span>
            <span class="n">cur_node</span> <span class="o">=</span> <span class="n">Node</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">rdmol</span><span class="p">,</span> <span class="n">get_atom_attributes</span><span class="p">,</span> <span class="n">has_3D</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">nodes</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">cur_node</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">has_3D</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">xyz</span><span class="p">[</span><span class="n">k</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">cur_node</span><span class="o">.</span><span class="n">pos_x</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">xyz</span><span class="p">[</span><span class="n">k</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">cur_node</span><span class="o">.</span><span class="n">pos_y</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">xyz</span><span class="p">[</span><span class="n">k</span><span class="p">,</span> <span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">cur_node</span><span class="o">.</span><span class="n">pos_z</span>

        <span class="n">adj_matrix</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">eye</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">num_nodes</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">edges</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">bond</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">rdmol</span><span class="o">.</span><span class="n">GetBonds</span><span class="p">()):</span>
            <span class="n">cur_edge</span> <span class="o">=</span> <span class="n">Edge</span><span class="p">(</span><span class="n">bond</span><span class="p">,</span> <span class="n">get_bond_attributes</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">edges</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">cur_edge</span><span class="p">)</span>
            <span class="n">adj_matrix</span><span class="p">[</span><span class="n">cur_edge</span><span class="o">.</span><span class="n">begin_atom_idx</span><span class="p">,</span> <span class="n">cur_edge</span><span class="o">.</span><span class="n">end_atom_idx</span><span class="p">]</span> <span class="o">=</span> <span class="mf">1.0</span>
            <span class="n">adj_matrix</span><span class="p">[</span><span class="n">cur_edge</span><span class="o">.</span><span class="n">end_atom_idx</span><span class="p">,</span> <span class="n">cur_edge</span><span class="o">.</span><span class="n">begin_atom_idx</span><span class="p">]</span> <span class="o">=</span> <span class="mf">1.0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">adj_matrix</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">max_size</span><span class="p">,</span> <span class="n">max_size</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">adj_matrix</span><span class="p">[:</span><span class="bp">self</span><span class="o">.</span><span class="n">num_nodes</span><span class="p">,</span> <span class="p">:</span><span class="bp">self</span><span class="o">.</span><span class="n">num_nodes</span><span class="p">]</span> <span class="o">=</span> <span class="n">adj_matrix</span>
        <span class="k">if</span> <span class="n">get_bond_attributes</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">edges</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">edges</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">n_attr</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">tmp</span><span class="o">.</span><span class="n">attributes_dict</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span>

<div class="viewcode-block" id="Graph.get_node_attr_adj_matrix"><a class="viewcode-back" href="../../../api-docs/utils.html#openchem.utils.graph.Graph.get_node_attr_adj_matrix">[docs]</a>    <span class="k">def</span> <span class="nf">get_node_attr_adj_matrix</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attr</span><span class="p">):</span>
        <span class="n">node_attr_adj_matrix</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">num_nodes</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">num_nodes</span><span class="p">,</span> <span class="n">attr</span><span class="o">.</span><span class="n">n_values</span><span class="p">))</span>
        <span class="n">attr_one_hot</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">node_idx</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="k">for</span> <span class="n">node</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">nodes</span><span class="p">:</span>
            <span class="n">tmp</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">one_hot_dict</span><span class="p">[</span><span class="n">node</span><span class="o">.</span><span class="n">attributes_dict</span><span class="p">[</span><span class="n">attr</span><span class="o">.</span><span class="n">name</span><span class="p">]]</span>
            <span class="n">attr_one_hot</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tmp</span><span class="p">)</span>
            <span class="n">node_attr_adj_matrix</span><span class="p">[</span><span class="n">node</span><span class="o">.</span><span class="n">node_idx</span><span class="p">,</span> <span class="n">node</span><span class="o">.</span><span class="n">node_idx</span><span class="p">]</span> <span class="o">=</span> <span class="n">tmp</span>
            <span class="n">node_idx</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">node_idx</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">edge</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">edges</span><span class="p">:</span>
            <span class="n">begin</span> <span class="o">=</span> <span class="n">edge</span><span class="o">.</span><span class="n">begin_atom_idx</span>
            <span class="n">end</span> <span class="o">=</span> <span class="n">edge</span><span class="o">.</span><span class="n">end_atom_idx</span>
            <span class="n">begin_one_hot</span> <span class="o">=</span> <span class="n">attr_one_hot</span><span class="p">[</span><span class="n">node_idx</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">begin</span><span class="p">)]</span>
            <span class="n">end_one_hot</span> <span class="o">=</span> <span class="n">attr_one_hot</span><span class="p">[</span><span class="n">node_idx</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">end</span><span class="p">)]</span>
            <span class="n">node_attr_adj_matrix</span><span class="p">[</span><span class="n">begin</span><span class="p">,</span> <span class="n">end</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="p">(</span><span class="n">begin_one_hot</span> <span class="o">+</span> <span class="n">end_one_hot</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span>

        <span class="k">return</span> <span class="n">node_attr_adj_matrix</span></div>

<div class="viewcode-block" id="Graph.get_edge_attr_adj_matrix"><a class="viewcode-back" href="../../../api-docs/utils.html#openchem.utils.graph.Graph.get_edge_attr_adj_matrix">[docs]</a>    <span class="k">def</span> <span class="nf">get_edge_attr_adj_matrix</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">all_atr_dict</span><span class="p">,</span> <span class="n">max_size</span><span class="p">):</span>
        <span class="n">fl</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">for</span> <span class="n">edge</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">edges</span><span class="p">:</span>
            <span class="n">begin</span> <span class="o">=</span> <span class="n">edge</span><span class="o">.</span><span class="n">begin_atom_idx</span>
            <span class="n">end</span> <span class="o">=</span> <span class="n">edge</span><span class="o">.</span><span class="n">end_atom_idx</span>
            <span class="n">cur_features</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">attr_name</span> <span class="ow">in</span> <span class="n">edge</span><span class="o">.</span><span class="n">attributes_dict</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
                <span class="n">cur_attr</span> <span class="o">=</span> <span class="n">all_atr_dict</span><span class="p">[</span><span class="n">attr_name</span><span class="p">]</span>
                <span class="k">if</span> <span class="n">cur_attr</span><span class="o">.</span><span class="n">one_hot</span><span class="p">:</span>
                    <span class="n">cur_features</span> <span class="o">+=</span> <span class="nb">list</span><span class="p">(</span><span class="n">cur_attr</span><span class="o">.</span><span class="n">one_hot_dict</span><span class="p">[</span><span class="n">edge</span><span class="o">.</span><span class="n">attributes_dict</span><span class="p">[</span><span class="n">cur_attr</span><span class="o">.</span><span class="n">name</span><span class="p">]])</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">cur_features</span> <span class="o">+=</span> <span class="p">[</span><span class="n">edge</span><span class="o">.</span><span class="n">attributes_dict</span><span class="p">[</span><span class="n">cur_attr</span><span class="o">.</span><span class="n">name</span><span class="p">]]</span>
            <span class="n">cur_features</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">cur_features</span><span class="p">)</span>
            <span class="n">attr_len</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">cur_features</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">fl</span><span class="p">:</span>
                <span class="n">edge_attr_adj_matrix</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">max_size</span><span class="p">,</span> <span class="n">max_size</span><span class="p">,</span> <span class="n">attr_len</span><span class="p">))</span>
                <span class="n">fl</span> <span class="o">=</span> <span class="kc">False</span>
            <span class="n">edge_attr_adj_matrix</span><span class="p">[</span><span class="n">begin</span><span class="p">,</span> <span class="n">end</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="n">cur_features</span>
            <span class="n">edge_attr_adj_matrix</span><span class="p">[</span><span class="n">end</span><span class="p">,</span> <span class="n">begin</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="n">cur_features</span>

        <span class="k">return</span> <span class="n">edge_attr_adj_matrix</span></div>

<div class="viewcode-block" id="Graph.get_node_feature_matrix"><a class="viewcode-back" href="../../../api-docs/utils.html#openchem.utils.graph.Graph.get_node_feature_matrix">[docs]</a>    <span class="k">def</span> <span class="nf">get_node_feature_matrix</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">all_atr_dict</span><span class="p">,</span> <span class="n">max_size</span><span class="p">):</span>
        <span class="n">features</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">node</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">nodes</span><span class="p">:</span>
            <span class="n">cur_features</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">attr_name</span> <span class="ow">in</span> <span class="n">node</span><span class="o">.</span><span class="n">attributes_dict</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
                <span class="n">cur_attr</span> <span class="o">=</span> <span class="n">all_atr_dict</span><span class="p">[</span><span class="n">attr_name</span><span class="p">]</span>
                <span class="k">if</span> <span class="n">cur_attr</span><span class="o">.</span><span class="n">one_hot</span><span class="p">:</span>
                    <span class="n">cur_features</span> <span class="o">+=</span> <span class="nb">list</span><span class="p">(</span><span class="n">cur_attr</span><span class="o">.</span><span class="n">one_hot_dict</span><span class="p">[</span><span class="n">node</span><span class="o">.</span><span class="n">attributes_dict</span><span class="p">[</span><span class="n">cur_attr</span><span class="o">.</span><span class="n">name</span><span class="p">]])</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">cur_features</span> <span class="o">+=</span> <span class="p">[</span><span class="n">node</span><span class="o">.</span><span class="n">attributes_dict</span><span class="p">[</span><span class="n">cur_attr</span><span class="o">.</span><span class="n">name</span><span class="p">]]</span>
            <span class="n">features</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">cur_features</span><span class="p">)</span>

        <span class="n">features</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">features</span><span class="p">)</span>
        <span class="n">padded_features</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">max_size</span><span class="p">,</span> <span class="n">features</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
        <span class="n">padded_features</span><span class="p">[:</span><span class="n">features</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="p">:</span><span class="n">features</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]]</span> <span class="o">=</span> <span class="n">features</span>
        <span class="k">return</span> <span class="n">padded_features</span></div>
    
    
<div class="viewcode-block" id="Graph.xyz_to_zmat"><a class="viewcode-back" href="../../../api-docs/utils.html#openchem.utils.graph.Graph.xyz_to_zmat">[docs]</a>    <span class="k">def</span> <span class="nf">xyz_to_zmat</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span></div>
        
<div class="viewcode-block" id="Graph.zmat_to_xyz"><a class="viewcode-back" href="../../../api-docs/utils.html#openchem.utils.graph.Graph.zmat_to_xyz">[docs]</a>    <span class="k">def</span> <span class="nf">zmat_to_xyz</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span></div></div>
    
        

</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, Mariya Popova

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
    

  <style>
    /* Sidebar header (and topbar for mobile) */
    .wy-side-nav-search, .wy-nav-top {
      background: #99badd;
    }
  </style>


</body>
</html>